package com.cskaoyan.dbutills;

import com.cskaoyan.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * 创建日期: 2022/10/11 11:47
 *
 * @author ciggar
 */
public class QueryRunnerDemo1 {

    static DataSource dataSource;
    static {
        dataSource = DruidUtils.getDataSource();
    }

    public static void main(String[] args) throws SQLException {


        // 1. 创建QueryRunner实例对象

        // 无参构造
        QueryRunner queryRunner = new QueryRunner();

        // 有参构造（传入了数据库连接池的）
        QueryRunner queryRunnerWithDatasource = new QueryRunner(dataSource);



        // 2. 使用QueryRunner执行sql语句
        Connection connection = DruidUtils.getConnection();
        int affectedRows = queryRunner.update(connection,"insert into account values (?,?,?)",5001,"黄蓉",500);
//        System.out.println(affectedRows);

        int afffectedRows2 = queryRunnerWithDatasource.update("insert into account values (?,?,?)", 6001, "郭靖", 500);
        System.out.println(afffectedRows2);

    }
}
